﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using PriceLibrary.PriceService;

namespace VDataAdapter.Communication
{
    class StockMessageBuilder
    {
        private static string EncodeTime(string input)
        {
            int time = int.Parse(input);
            if (time == 0)
                return DateTime.Now.ToString("hh:mm:ss") + "|";
            else
                return time.ToString("00:00:00") + "|" ?? DBNull.Value.ToString() + "|";
        }

        /// <summary>
        /// Emptry string or Null will return "Null"
        /// Specicial charactesr will be removed
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private static string EncodeStringProperty(string input)
        {
            string result = EncodeEmptyOrNullString(input);
            return RemoveSpecialCharacter(result);

        }
        /// <summary>
        /// Return "Null" for such cases
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private static string EncodeEmptyOrNullString(string input)
        {
            if (input == null)
                return "Null";

            if (input == String.Empty)
                return "Null";

            return input;
        }

        /// <summary>
        /// Translate a StockInfoDyns into a string in correct format
        /// if property is null then the name of the property is given instead
        /// </summary>
        /// <param name="stock"></param>
        /// <returns></returns>
        public static string BuildMessage(StockInfoDyns stock)
        {
            string result = String.Empty;
             
            result +=
            stock.AccumulatedVal
            + "|" + stock.AccumulatedVol
            + "|" + stock.AdjustQtty
            + "|" + stock.AdjustRate
            + "|" + stock.AvrPrice
            + "|" + stock.Best1Bid
            + "|" + stock.Best1BidVolume
            + "|" + stock.Best1Offer
            + "|" + stock.Best1OfferVolume
            + "|" + stock.Best2Bid
            + "|" + stock.Best2BidVolume
            + "|" + stock.Best2Offer
            + "|" + stock.Best2OfferVolume
            + "|" + stock.Best3Bid
            + "|" + stock.Best3BidVolume
            + "|" + stock.Best3Offer
            + "|" + stock.Best3OfferVolume
            + "|" + stock.BidCount
            + "|" + stock.BuyForeignQtty
            + "|" + stock.BuyForeignValue//  + " BuyForeignValue"
            + "|" + stock.ClosePrice// + " Close Price"
            + "|" + stock.CurrentRoom
            + "|" + stock.DividentRate
            + "|" + stock.Highest
            + "|" + stock.Last
            + "|" + stock.LastVal
            + "|" + stock.LastVol
            + "|" + stock.Lowest
            + "|" + stock.OfferCount
            + "|" + stock.OpenPrice
            + "|" + stock.ProjectOpen
            + "|" + stock.PtMatchPrice
            + "|" + stock.PtMatchQtty
            + "|" + stock.PtTotalTradedQtty
            + "|" + stock.PtTotalTradedValue
            + "|" + stock.ReferenceStatus
            + "|" + stock.SellForeignQtty
            + "|" + stock.SellForeignValue
            + "|" + stock.Status
            + "|" + EncodeStringProperty(stock.StockSymbol)
            + "|" + EncodeStringProperty(EncodeTime(stock.Time))
            + "|" + stock.TotalBidQtty
            + "|" + stock.TotalOfferQtty
            + "|" + stock.TotalRoom
            + "|" + stock.TotalShare
            + "|" + stock.TotalValue
            + "|";

            return result;
        }

        /// <summary>
        /// for code 2 must return "02"
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private static string GetFloorCode(int code)
        {
            if (code < 10 && code > 0)
                return "0" + code;
            else
                return code.ToString();
        }

        private static string RemoveSpecialCharacter(string input)
        {
            return input.Trim().Replace("|", " ").Replace("#", " ");
        }

        public static string BuildMessage(StockInfoStatic stock)
        {
            string result = string.Empty;
             
            result +=
            stock.BasicPrice
            + "|" + stock.Ceiling
            + "|" + stock.DateNo
            + "|" + stock.Floor
            + "|" + GetFloorCode(stock.FloorCode)
            + "|" + stock.ListingStatus
            + "|" + stock.PrevPriorPrice
            + "|" + stock.PriorClosePrice
            + "|" + stock.PriorPrice
            + "|" + EncodeStringProperty(stock.SecurityName)
            + "|" + EncodeStringProperty(stock.StockType)
            + "|" + stock.TotalListingQtty
            + "|" + stock.TradingUnit
            + "|";

            return result;
        }

        public static string BuildMessage(StockInfoStatic objStatic, StockInfoDyns objNew, StockInfoDyns objOld)
        {
            if (objStatic.StockSymbol == "SCC")            
            {
                Console.Write("test");
            }

            string result;
            try
            {
                result = MessageType.Stock.ToString().ToUpper() + "#";
                string strDynsMsgNew = StockMessageBuilder.BuildMessage(objNew);
                string strDynsMsgOld = StockMessageBuilder.BuildMessage(objOld);
                string strStaticMsg = StockMessageBuilder.BuildMessage(objStatic);
                ///Phân cách giữa Msg mới và Msg cũ bằng dấu #
                result += String.Format("{0}{2}#{0}{1}", strStaticMsg, strDynsMsgOld, strDynsMsgNew);
                //_log.WarnFormat("{0}", strMsgObj); 
            }
            catch (Exception ex)
            {
                ex.Data["StockInfoStatic"] = objStatic;
                ex.Data["new StockInfoDyns"] = objNew;
                ex.Data["old StockInfoDyns"] = objOld;
                throw ex;
            }

            return result;
        }

    }
}
